<?php
namespace  zhtfrontend\components\services\orders;

use common\components\helpers\Fun;
use common\models\datas\CardAttr;
use common\models\datas\Reservation;
use common\models\datas\Student;
use yii\base\Component;
use zhtfrontend\models\datas\Coupons;
use zhtfrontend\models\datas\CustomerCoupons;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsPrice;
use zhtfrontend\models\datas\GoodsTeacher;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\OrdersItemsRelation;

class Operorders extends Component
{
    //修改订单商品付款价格
    public static function UpdateOrderItem($orers_id,$discount_type,$discount){

        $orders = Orders::findOne($orers_id);
        $orderitems = OrdersItems::find()->where(['orders_id'=>$orers_id])->all();
        $orderRelation = OrdersItemsRelation::find()->where(['orders_id'=>$orers_id])->one();
//        print_r($orderitems);die;
        if(isset($discount_type) && $discount_type == 'scholarship'){
//           $proportion_scholarship_money =  $orders->scholarship_money/$orders->final_amount;
           $proportion_account_money =  $orders->account_money/$orders->final_amount;
          // var_dump($proportion_scholarship_money);var_dump($proportion_account_money);die;
            $use_scholarship_money = $orders->scholarship_money;
            foreach ($orderitems as $item){
                $scholarship_money = 0;
                if($item->teach_type == 1){
                    if($orderRelation->is_scholarship == 1){
                        if($use_scholarship_money >= $orderRelation->scholarship){
                            $scholarship_money = $orderRelation->scholarship;
                            $use_scholarship_money -= $orderRelation->scholarship;
                        }else{
                            if($use_scholarship_money > 0) {
                                $scholarship_money = $use_scholarship_money;
                                $use_scholarship_money = 0;
                            }
                        }
                    }
                }else{
                    if($orderRelation->is_art_scholarship == 1){
                        if($use_scholarship_money >= $orderRelation->art_scholarship){
                            $scholarship_money = $orderRelation->art_scholarship;
                            $use_scholarship_money -= $orderRelation->art_scholarship;
                        }else{
                            if($use_scholarship_money > 0){
                                $scholarship_money = $use_scholarship_money;
                                $use_scholarship_money = 0;
                            }
                        }
                    }
                }
                $item->scholarship_money = $scholarship_money;
                $item->cash_money = $item->amount*$proportion_account_money;
                $item->actual_scholarship_money = $scholarship_money;
                $item->actual_cash_money = $item->amount*$proportion_account_money;

                $item->save(false);
            }
        }elseif (isset($discount_type) && $discount_type == 'coupon'){
            $cusCoupons = CustomerCoupons::findOne($discount);
            $coupons = Coupons::findOne($cusCoupons->coupon_id);
            if($coupons->type == 'full'){
                $discount_amount = 1-($coupons->amount/$orders->total_amount);
            }else{
                $discount_amount = $coupons->amount/100;
            }
            foreach ($orderitems as $item){
                $item->cash_money = $item->amount*$discount_amount;
                $item->actual_cash_money =  $item->amount*$discount_amount;
                $item->discount =  $item->amount*(1-$discount_amount);
                $item->save(false);
            }
        }else{
            foreach ($orderitems as $item){
                $item->cash_money = $item->amount;
                $item->actual_cash_money = $item->amount;
                $item->save(false);
            }
        }

        //减少库存
        $orderitemsRow = OrdersItems::find()->where(['orders_id'=>$orers_id,'teach_type'=>1])->one();
        if($orderitemsRow != null && $orders->order_type != 'renew'){
            $goodsRow = Goods::findOne($orderitemsRow->goods_id);
            if($goodsRow != null){
                $goodsRow->store -= 1;
                $goodsRow->save(false);
            }
        }

        //减少活动人数
//        $price = GoodsPrice::findOne($orderRelation->goods_price_id);
//        if($price != null) {
//            if ($price->activity_start_time <= time() && $price->activity_end_time >= time() && $price->effective_number > 0) {
//                $price->effective_number -= 1;
//                $price->save(false);
//            }
//        }


    }

    public static function addReservation($model){
        $orderitems = OrdersItems::find()->where(['orders_id'=>$model->id,'teach_type'=>1])->one();
        $student = Student::find()->where(['customer_id'=>$model->customer_id])->one();

        $teacher_ids = '';
        $teacherArr = GoodsTeacher::find()->select('teacher_id')->where(['goods_id'=>$orderitems->goods_id])->column();
        if(!empty($teacherArr)){
            $teacher_ids = implode(',',$teacherArr);
        }
        $goods = Goods::findOne($orderitems->goods_id);
        $cardAttr = CardAttr::find()->where(['customer_id'=>$model->customer_id,'site_id'=>Fun::getSiteId()])->one();
        $age = 0;
        if(!empty($student->birthday)){
            $arr = explode('-',$student->birthday);
            $age = date('Y')-$arr[0];
        }
        $salesman_id = 0;
        if($cardAttr != null){
            $salesman_id = $cardAttr->salesman_id;
        }

        $resModel = new Reservation();
        $resModel->customer_id = $model->customer_id;
        $resModel->student_id = $student->id;
        $resModel->mechanic_id = $orderitems->mechanic_id;
        $resModel->goods_id = $orderitems->goods_id;
        $resModel->teacher_id = $teacher_ids;
        $resModel->cate_id = $goods->cat_id;
        $resModel->orders_id = $model->id;
        $resModel->orders_items_id = $orderitems->id;
        $resModel->created_at = time();
        $resModel->salesman_id = $salesman_id;
        $resModel->mechanic_area = '李沧区';
        $resModel->site_id = Fun::getSiteId();
        $resModel->student_age = $age;
        $resModel->save(false);
    }

}